package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO 购物车数据
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 先查询购物车是否存在当前菜品/套餐，不存在添加菜品/套餐，存在仅更新数量++
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        // 根据条件查询购物车列表
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectList(shoppingCart);
        // 更新菜品/套餐数量
        if(shoppingCartList != null && !shoppingCartList.isEmpty()) {
            ShoppingCart cart = shoppingCartList.get(0);
            cart.setNumber(cart.getNumber() + 1);
            // 根据购物车id更新数量
            shoppingCartMapper.updateNumberById(cart);
            return;
        }
        // 添加菜品/套餐
        Long dishId = shoppingCartDTO.getDishId();
        Long setmealId = shoppingCartDTO.getSetmealId();
        if(dishId != null && setmealId == null) {
            // 根据菜品id查询菜品
            Dish dish = dishMapper.getById(dishId);
            // 设置购物车菜品基本信息
            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
            shoppingCart.setAmount(dish.getPrice());
            log.info("添加菜品到购物车：{}", shoppingCart);
        } else if(dishId == null && setmealId != null) {
            // 根据套餐id查询套餐
            Setmeal setmeal = setmealMapper.selectById(setmealId);
            // 设置购物车套餐基本信息
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setImage(setmeal.getImage());
            shoppingCart.setAmount(setmeal.getPrice());
            log.info("添加套餐到购物车：{}", shoppingCart);
        } else {
            throw new RuntimeException("参数有误，添加购物车失败！");
        }
        shoppingCart.setNumber(1); // 设置第一次默认添加数量为1
        shoppingCart.setCreateTime(LocalDateTime.now());
        // 添加购物车操作
        shoppingCartMapper.insert(shoppingCart);
    }

    /**
     * 删减购物车
     *
     * @param shoppingCartDTO 购物车数据
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 先查询购物车当前菜品/套餐的数量，数量为1时删除菜品/套餐，大于1时仅更新数量--
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        // 根据条件查询购物车列表
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectList(shoppingCart);
        if(shoppingCartList == null || shoppingCartList.isEmpty()) return;

        // 获取购物车数据
        ShoppingCart cart = shoppingCartList.get(0);
        Integer number = cart.getNumber();
        // 更新菜品/套餐数量
        if(number != 1) {
            // 更新数量--
            cart.setNumber(number - 1);
            // 根据购物车id更新数量
            shoppingCartMapper.updateNumberById(cart);
            return;
        }
        // 根据id删除菜品/套餐
        shoppingCartMapper.deleteById(cart.getId());
    }

    /**
     * 查询购物车列表
     *
     * @return 购物车列表
     */
    @Override
    public List<ShoppingCart> getShoppingCartList() {
        // 根据用户id查询购物车列表
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        return shoppingCartMapper.selectList(shoppingCart);
    }

    /**
     * 清空购物车
     */
    @Override
    public void cleanShoppingCart() {
        // 根据用户id删除购物车列表
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }
}
